<?php
class document extends DOMDocument
{
	/**
	 * 文件
	 *
	 * @var string
	 */
	protected $file;

	/**
	 * simplexml模型
	 *
	 * @var object
	 */
	protected $sxml;

	/**
	 * 根节点
	 *
	 * @var object
	 */
	protected $root;

	/**
	 * xpath
	 *
	 * @var object
	 */
	protected $xpath;

	/**
	 * 是否HTML文档
	 *
	 * @var bool
	 */
	protected $isHtml = false;

	/**
	 * 是否保存文件
	 *
	 * @var bool
	 */
	protected $isSave = false;

	/**
	 * 是否为文件数据流
	 *
	 * @var bool
	 */
	protected $isFile = false;

	/**
	 * 根节点名
	 *
	 * @var string
	 */
	public $rootTagName = '';

	/**
	 * 构造函数
	 *
	 * @param mixed $string 
	 * @param string $file
	 * @param bool $isSave
	 * @param bool $isHtml
	 */
	function __construct( $string = '', $file = '', $isSave = false, $isHtml = false )
	{
		parent::__construct('1.0', 'utf-8');
		$this->formatOutput = true;
		$this->encoding = 'utf-8';
		$this->file = $file;
		$this->isSave = $isSave;
		$this->isHtml = $isHtml;
		$aFile = split('[.]',$file);
		$filetye = end($aFile);
		if ( is_array( $string ) ) {
			$string = $this->array_to_xml( $string );
		}
		if ( $file ){
			if (file_exists($file)){
				$this->load_file( $file );
			}
			else {
				if ( $string ){
					$this->load_data( $string );
					if ( $this->isSave ) $this->save_file();
				}
				else {
					$root = $this->createElement( 'html' );
					$root = $this->appendChild( $root );
					$this->save_file();
					$this->load_file( $file );
				}
			}
			$this->isFile = true;
		}
		else {
			if ( $string ){
				$this->load_data( $string );
				if ( $this->isSave ) $this->save_file();
			}
			else {
				$string = '<html></html>';
				$this->load_data( $string );
			}
		}
		@$this->sxml = simplexml_import_dom( $this );
		$this->root = $this->documentElement;
		$this->rootTagName = $this->root->tagName;
		$this->xpath = new DOMXPath($this);
	}

	/**
	 * Enter description here...
	 *
	 * @param string $fun_name
	 * @param mixed $arg
	 * @return null
	 */
	function __call( $fun_name, $arg )
	{
		return null;
	}

	/**
	 * 删除元素
	 *
	 * @param string $node 选取的元素#id .class tag   [property="value"]
	 * @return bool
	 */
	function remove_node( $node )
	{
		$node = $this->get_element( $node );
		if ( $node ){
			if ( $node->length > 1){
				$i =  $node->length;
				while ( $i > 0 ) {
					$i--;
					$entry = $node->item( $i );
					if ($root = $entry->parentNode ){
						if ( !$root->removeChild( $entry ) ) return false;
					}
					else {
						return false;
					}
				}
			}
			else{
				if ( $root = $node->parentNode ){
					if ( !$root->removeChild( $node ) ) return false;
				}
				else {
					return false;
				}
			}
			return $this->save_file();
		}
		return false;
	}

	/**
	 * 删除一个节点下的子节点
	 *
	 * @param string $node 选取的元素#id .class tag   [property="value"]
	 * @param string $tag 待删除节点#id .class tag   [property="value"]
	 * @return bool
	 */
	function remove_childs( $node = '', $tag = '' )
	{
		$node = $this->get_element( $node );
		if ( $node ){
			if ( $node->length > 1 ){
				$i = $node->length;
				while ( $i > 0 ) {
					$i--;
					$root = $node->item( $i );
					if ( !$this->remove_childs_data( $root, $tag ) ) return false;
				}
			}
			else {
				if ( !$this->remove_childs_data( $node, $tag ) ) return false;
			}
			return $this->save_file();
		}
		return false;
	}

	/**
	 * remove_childs_data
	 *
	 * @param object $node
	 * @param string $tag
	 * @return bool
	 */
	private function remove_childs_data( $node, $tag = '' )
	{
		$childs = $this->evaluate( $tag, false, $node);
		if ( $childs->length > 1 ) {
			$i = $childs->length;
			while ( $i > 0 ) {
				$i--;
				$child = $childs->item( $i );
				if ( !$child->parentNode->removeChild( $child ) ) return false;
			}
		}
		else {
			if ( !$childs->parentNode->removeChild( $childs ) ) return false;
		}
		return true;
	}

	/**
	 * 设置节点文本内容
	 *
	 * @param string $node 选取的元素#id .class tag   [property="value"]
	 * @param string $data 内容(包含的HTML字符将会被编码)
	 * @return bool
	 */
	function text( $node = false, $data = '' )
	{
		$node = $this->get_element( $node );
		if ( $node->length > 1 ) {
			foreach ( $node as $entry ) {
				if ( !$this->text_data( $entry, $data ) ) return false;
			}
		}
		else {
			if ( !$this->text_data( $node, $data ) ) return false;
		}
		return $this->save_file();
	}

	/**
	 * text_data
	 *
	 * @return bool
	 */
	private function text_data( $node, $data = '' )
	{
		$text = $this->createTextNode( $data );
		if ( $node->childNodes->length > 1 ) {
			while ( $node->hasChildNodes()) {
				if ( !$node->removeChild( $node->childNodes->item( 0 ) ) ) return false;
			}
			if ( !$node->appendChild( $text ) ) return false;
		}
		else {
			if ( !$node->replaceChild( $text, $node->firstChild ) ) return false;
		}
		return true;
	}

	/**
	 * 在指定的元素内搜索元素
	 *
	 * @param string $tag 元素#id .class tag   [property="value"]
	 * @param mixed $item 返回的节点索引
	 * @param object $node 指定的节点
	 * @return object
	 */
	function evaluate( $tag = '', $item = false, $node = false ){
		$node or  $node = $this->root;
		if ( $tag ) {
			$aTag = preg_split( '/\[/', $tag );
			foreach ( $aTag as $key => $value ) {
				if ( $key == 0 ){
					if ( $value ){
						if ( preg_match('/^#/',$value) ){
							$query = './/*[@id="'. trim( $value, '#' ) .'"]';
						}
						elseif ( preg_match('/^\./',$tag) ) {
							$query = './/*[@class="'. trim( $value, '.' ) .'"]';
						}
						else {
							$query = './/'. $value;
						}
					}
					else {
						$query = './/*';
					}
				}
				else {
					$query .= '[@'. $value;
				}
			}
			$entrys = $this->xpath->evaluate( $query, $node);
		}
		else {
			$entrys = $node;
		}
		if ( $entrys ) {
			if ( $item !== false ) {
				if ( $entrys->item( $item ) ) return $entrys->item( $item );
			}
			else {
				if ( $entrys->length == 1 ) return $entrys->item( 0 );
				else return $entrys;
			}
		}
		return null;
	}

	/**
	 * 查询节点
	 *
	 * @param string $query 节点路径
	 * @param mixed $item 返回的节点索引
	 * @return object
	 */
	function query( $query, $item = false )
	{
		$entrys = $this->xpath->query( $query );
		if ( $entrys ) {
			if ( $item !== false ) {
				if ( $entrys->item( $item ) ) return $entrys->item( $item );
			}
			else {
				if ( $entrys->length == 1 ) return $entrys->item( 0 );
				else return $entrys;
			}
		}
		return null;
	}

	/**
	 * 获取节点中的文本
	 *
	 * @param string $node 选取的元素#id .class tag   [property="value"]
	 * @return mixed
	 */
	function get_text( $node )
	{
		$node = $this->get_element( $node );
		if ( $node->length > 1 ){
			$aResult = array();
			foreach ( $node as $entry ) {
				$i = $entry->childNodes->length;
				if ( $i > 1) {
					$j = 0;
					while ( $j < $i ){
						if ( !$entry->childNodes->item( $j )->tagName ) {
							$result .= $entry->childNodes->item( $j )->nodeValue;
						}
						$j++;
					}
				}
				else {
					if ( !$entry->firstChild->tagName ) {
						$result = $entry->firstChild->nodeValue;
					}
				}
				$aResult[] = $result;
			}
			return $aResult;
		}
		$i = $node->childNodes->length;
		if ( $i > 1) {
			$j = 0;
			while ( $j < $i ){
				if ( !$node->childNodes->item( $j )->tagName ) {
					$result .= $node->childNodes->item( $j )->nodeValue;
				}
				$j++;
			}
		}
		else {
			if ( !$node->firstChild->tagName ) {
				$result = $node->firstChild->nodeValue;
			}
		}
		return $result;
	}

	/**
	 * 设置节点属性
	 *
	 * @param string $node 选取的元素#id .class tag   [property="value"]
	 * @param string $name 属性名
	 * @param sting $value 属性值
	 * @return bool
	 */
	function set_attribute( $node = '', $name, $value='' )
	{
		if ( !$name ) return false;
		$node = $this->get_element( $node );
		if ( $node->length > 1 ) {
			foreach ( $node as $entry ) {
				if ( !$this->set_attribute_data( $entry, $name, $value ) ) return false;
			}
		}
		else {
			if ( !$this->set_attribute_data( $node, $name, $value ) ) return false;
		}
		return $this->save_file();
	}

	/**
	 * set_attribute_data
	 *
	 * @param object $node
	 * @param string $name
	 * @param string $value
	 * @return bool
	 */
	private function set_attribute_data( $node, $name, $value = '')
	{
		if ( !$node->setAttribute( $name, $value ) ) return false;
		return true;
	}

	/**
	 * 删除节点属性
	 *
	 * @param string $node 选取的元素#id .class tag   [property="value"]
	 * @param string $name 待删除属性名
	 * @return bool
	 */
	function remove_attribute( $node = '', $name )
	{
		if ( !$name ) return false;
		$node = $this->get_element( $node );
		if ( $node->length > 1 ) {
			foreach ( $node as $entry ) {
				if ( !$this->remove_attribute_data( $entry, $name ) ) return false;
			}
		}
		else {
			if ( !$this->remove_attribute_data( $node, $name ) ) return false;
		}
		return $this->save_file();
	}

	/**
	 * remove_attribute_data
	 *
	 * @param object $node
	 * @param string $name
	 * @return bool
	 */
	private function remove_attribute_data( $node, $name )
	{
		if ( $node->hasAttribute( $name ) ){
			if ( !$node->removeAttribute( $name ) ) return false;
		}
		return true;
	}

	/**
	 * 获取节点属性
	 *
	 * @param string $node 选取的元素#id .class tag   [property="value"]
	 * @param string $name 属性名
	 * @return string
	 */
	function get_attribute( $node = '', $name )
	{
		if( !$name ) return null;
		$node = $this->get_element( $node );
		if ( $node->length > 1 ) $node = $node->item( 0 );
		if ( $node->hasAttribute( $name ) ){
			return $node->getAttribute( $name );
		}
		return null;
	}

	/**
	 * 设置元素css(html)
	 *
	 * @param string $node 选取的元素#id .class tag   [property="value"]
	 * @param mixed $data css属性
	 * @return bool
	 */
	function css( $node, $data = '' )
	{
		$node = $this->get_element( $node );
		if ( $node->length > 1 ) {
			foreach ( $node as $entry ) {
				if ( !$this->css_data( $entry, $data ) ) return false;
			}
		}
		else {
			if ( !$this->css_data( $node, $data ) ) return false;
		}
		return $this->save_file();
	}

	/**
	 * css_data
	 *
	 * @param object $node
	 * @param mixed $data
	 * @return bool
	 */
	private function css_data( $node, $data = '' )
	{
		if ( $node->hasAttribute( 'style' ) ){
			$style = $node->getAttribute( 'style' );
			$aStyle = spliti( ';', $style );
			foreach ( $aStyle as $key => $value ) {
				if ( $value ) {
					$aCss = spliti( ':', $value );
					$aStyle[ $aCss[0] ] = $aCss[1];
					unset( $aStyle[$key] );
				}
				else {
					unset( $aStyle[$key] );
				}
			}
			if ( is_string( $data ) ){
				$aData = spliti( ';', $data );
				foreach ( $aData as $key => $value ) {
					if ( $value ) {
						$aCss = spliti( ':', $value );
						$aData[ $aCss[0] ] = $aCss[1];
						unset( $aData[$key] );
					}
					else {
						unset( $aData[$key] );
					}
				}
				$aStyle = array_merge( $aStyle, $aData );
			}
			else {
				if ( is_array( $data )) {
					$aStyle = array_merge( $aStyle, $data );
				}
				else {
					return false;
				}
			}
		}
		else {
			if ( is_string( $data ) ){
				$aData = spliti( ';', $data );
				foreach ( $aData as $key => $value ) {
					if ( $value ) {
						$aCss = spliti( ':', $value );
						$aData[ $aCss[0] ] = $aCss[1];
						unset( $aData[$key] );
					}
					else {
						unset( $aData[$key] );
					}
				}
				$aStyle = $aData;
			}
			else {
				if ( is_array( $data )) {
					$aStyle = $data;
				}
				else {
					return false;
				}
			}
		}
		$style = '';
		if ( is_array( $aStyle ) ) {
			foreach ( $aStyle as $key => $value ) {
				if ( is_numeric( $key ) ) continue;
				$style .= $key .":". $value .";";
			}
		}
		else {
			return false;
		}
		return $this->set_attribute_data( $node, 'style', $style );
	}

	/**
	 * 克隆一个节点中的子节点到另外一个节点中
	 *
	 * @param string $root 克隆节点#id .class tag   [property="value"]
	 * @param string $node 被克隆节点#id .class tag   [property="value"]
	 * @param mixed $item 被克隆子节点索引
	 * @return bool
	 */
	function clone_child( $root = '', $node = '', $item = false )
	{
		$root = $this->get_element( $root );
		$node = $this->get_element( $node );
		if ( !$root->hasChildNodes() ) return false;
		if ( $node->length > 1 ){
			foreach ( $node as $entry ) {
				if ( !$entry->appendChild( $root->childNodes->item( ( $item ? ( $root->childNodes->length > $item ? $item : 0) : 0 ) )->cloneNode( true ) ) ) return false;
			}
		}
		else {
			if ( !$node->appendChild( $root->childNodes->item( ( $item ? ( $root->childNodes->length > $item ? $item : 0) : 0 ) )->cloneNode( true ) ) ) return false;
		}
		return $this->save_file();
	}

	/**
	 * 在指定的节点之前插入一个节点
	 *
	 * @param string $node 选取的元素#id .class tag   [property="value"]
	 * @param string $data 待插入节点内容
	 * @return bool
	 */
	function before_insert( $node = '', $data = '' )
	{
		$node = $this->get_element( $node );
		if ( $node->length > 1 ){
			foreach ( $node as $item ) {
				if ( !$this->before_insert_data( $item, $data ) ) return false;
			}
		}
		else {
			if ( !$this->before_insert_data( $node, $data ) ) return false;
		}
		return $this->save_file();
	}

	/**
	 * befor_insert_data
	 *
	 * @param object $node
	 * @param string $data
	 * @return bool
	 */
	private function before_insert_data( $node, $data = '' )
	{
		if ( $sxml = @simplexml_load_string( $data ) ) {
			if ( !$dom_sxml = @dom_import_simplexml( $sxml ) ) return false;
			$dom_sxml = $this->importNode( $dom_sxml, true);
			if ( !$node->parentNode->insertBefore( $dom_sxml, $node ) ) return false;
		}
		else {
			$dom = new DOMDocument();
			if ( $dom->loadXML( "<data>". $data ."</data>" ) ){
				$root = $dom->documentElement;
				if ( $root->childNodes->length > 1 ) {
					foreach ( $root->childNodes as $entry ) {
						$sxml = simplexml_import_dom( $entry );
						if ( !$dom_sxml = @dom_import_simplexml( $sxml ) ) return false;
						$dom_sxml = $this->importNode( $dom_sxml, true);

						if ( !$node->parentNode->insertBefore( $dom_sxml, $node ) ) return false;
					}
				}
				else {
					if ( !$root->firstChild->tagName ) {
						if ( !$text = $this->createTextNode( $data ) ) return false;

						if ( !$node->parentNode->insertBefore( $text, $node ) ) return false;
					}
					else {
						return false;
					}
				}
			}
			else {
				return false;
			}
		}
		return true;
	}

	/**
	 * 在指定的节点之后插入一个节点
	 *
	 * @param string $node 选取的元素#id .class tag   [property="value"]
	 * @param string $data 待插入节点内容
	 * @return bool
	 */
	function after_insert( $node = '', $data = '' )
	{
		$node = $this->get_element( $node );
		if ( $node->length > 1 ){
			foreach ( $node as $item ) {
				if ( !$this->after_insert_data( $item, $data ) ) return false;
			}
		}
		else {
			if ( !$this->after_insert_data( $node, $data ) ) return false;
		}
		return $this->save_file();
	}

	/**
	 * after_insert_data
	 *
	 * @param object $node
	 * @param string $data
	 * @return bool
	 */
	private function after_insert_data( $node, $data = '' )
	{
		if ( $sxml = @simplexml_load_string( $data ) ) {
			if ( !$dom_sxml = @dom_import_simplexml( $sxml ) ) return false;
			$dom_sxml = $this->importNode( $dom_sxml, true);

			$nextNode = $node->nextSibling;
			if ( $nextNode ){
				if ( !$node->parentNode->insertBefore( $dom_sxml, $nextNode ) ) return false;
			}
			else {
				if ( !$node->parentNode->appendChild( $dom_sxml ) ) return false;
			}
		}
		else {
			$dom = new DOMDocument();
			if ( $dom->loadXML( "<data>". $data ."</data>" ) ){
				$root = $dom->documentElement;
				if ( $root->childNodes->length > 1 ) {
					foreach ( $root->childNodes as $entry ) {
						$sxml = simplexml_import_dom( $entry );
						if ( !$dom_sxml = @dom_import_simplexml( $sxml ) ) return false;
						$dom_sxml = $this->importNode( $dom_sxml, true);

						$nextNode = $node->nextSibling;
						if ( $nextNode ){
							if ( !$node->parentNode->insertBefore( $dom_sxml, $nextNode ) ) return false;
						}
						else {
							if ( !$node->parentNode->appendChild( $dom_sxml ) ) return false;
						}
					}
				}
				else {
					if ( !$root->firstChild->tagName ) {
						if ( !$text = $this->createTextNode( $data ) ) return false;

						$nextNode = $node->nextSibling;
						if ( $nextNode ){
							if ( !$node->parentNode->insertBefore( $text, $nextNode ) ) return false;
						}
						else {
							if ( !$node->parentNode->appendChild( $text ) ) return false;
						}
					}
					else {
						return false;
					}
				}
			}
			else {
				return false;
			}
		}
		return true;
	}

	/**
	 * 在指定的节点中起始位置插入一个节点
	 *
	 * @param string $node 选取的元素#id .class tag   [property="value"]
	 * @param string $data 待插入节点内容
	 * @return bool
	 */
	function insert_before( $node = '', $data = '' )
	{
		$node = $this->get_element( $node );
		if ( $node->length > 1 ){
			foreach ( $node as $item ) {
				if ( !$this->insert_before_data( $item, $data ) ) return false;
			}
		}
		else {
			if ( !$this->insert_before_data( $node, $data ) ) return false;
		}
		return $this->save_file();
	}

	/**
	 * insert_before_data
	 *
	 * @param object $node
	 * @param string $data
	 * @return bool
	 */
	private function insert_before_data( $node, $data = '')
	{
		if ( $sxml = @simplexml_load_string( $data ) ) {
			if ( !$dom_sxml = @dom_import_simplexml( $sxml ) ) return false;
			$dom_sxml = $this->importNode( $dom_sxml, true);
			$child = $node->firstChild;
			if ( $child ){
				if ( !$node->insertBefore( $dom_sxml, $child ) ) return false;
			}
			else {
				if ( !$node->appendChild( $dom_sxml ) ) return false;
			}
		}
		else {
			$dom = new DOMDocument();
			if ( $dom->loadXML( "<data>". $data ."</data>" ) ){
				$root = $dom->documentElement;
				if ( $root->childNodes->length > 1 ) {
					foreach ( $root->childNodes as $entry ) {
						$sxml = simplexml_import_dom( $entry );
						if ( !$dom_sxml = @dom_import_simplexml( $sxml ) ) return false;
						$dom_sxml = $this->importNode( $dom_sxml, true);
						$child = $node->firstChild;
						if ( $child ){
							if ( !$node->insertBefore( $dom_sxml, $child ) ) return false;
						}
						else {
							if ( !$node->appendChild( $dom_sxml ) ) return false;
						}
					}
				}
				else {
					if ( !$root->firstChild->tagName ) {
						if ( !$text = $this->createTextNode( $data ) ) return false;
						$child = $node->firstChild;
						if ( $child ){
							if ( !$node->insertBefore( $text, $child ) ) return false;
						}
						else {
							if ( !$node->appendChild( $text ) ) return false;
						}
					}
					else {
						return false;
					}
				}
			}
			else {
				return false;
			}
		}
		return true;
	}

	/**
	 * 在指定的节点中最后位置插入一个节点
	 *
	 * @param string $node 选取的元素#id .class tag   [property="value"]
	 * @param string $data 待插入节点内容
	 * @return bool
	 */
	function insert_after( $node = '', $data = '' )
	{
		return $this->insert( $node, $data );
	}

	/**
	 * 向指定的节点中插入数据
	 *
	 * @param string $node 选取的元素#id .class tag   [property="value"]
	 * @param string $data 内容
	 * @return bool
	 */
	function insert( $node = '', $data = '')
	{
		$node = $this->get_element( $node );
		if ( $node->length > 1 ){
			foreach ( $node as $item ) {
				if ( !$this->insert_data( $item, $data ) ) return false;
			}
		}
		else {
			if ( !$this->insert_data( $node, $data ) ) return false;
		}
		return $this->save_file();
	}

	/**
	 * insert_data
	 *
	 * @param object $node
	 * @param string $data
	 * @return bool
	 */
	private function insert_data( $node, $data = '' )
	{
		if ( $sxml = @simplexml_load_string( $data ) ) {
			if ( !$dom_sxml = @dom_import_simplexml( $sxml ) ) return false;
			$dom_sxml = $this->importNode( $dom_sxml, true);
			if ( !$node->appendChild( $dom_sxml ) ) return false;
		}
		else {
			$dom = new DOMDocument();
			if ( $dom->loadXML( "<data>". $data ."</data>" ) ){
				$root = $dom->documentElement;
				if ( $root->childNodes->length > 1 ) {
					foreach ( $root->childNodes as $entry ) {
						$sxml = simplexml_import_dom( $entry );
						if ( !$dom_sxml = @dom_import_simplexml( $sxml ) ) return false;
						$dom_sxml = $this->importNode( $dom_sxml, true);
						if ( !$node->appendChild( $dom_sxml ) ) return false;
					}
				}
				else {
					if ( !$root->firstChild->tagName ) {
						if ( !$text = $this->createTextNode( $data ) ) return false;
						if ( !$node->appendChild( $text ) ) return false;
					}
					else {
						return false;
					}
				}
			}
			else {
				return false;
			}
		}
		return true;
	}

	/**
	 * 重新填充元素中的内容
	 *
	 * @param string $node 选取的元素#id .class tag   [property="value"]
	 * @param string $data 内容
	 * @return bool
	 */
	function fill( $node = '', $data = '')
	{
		$node = $this->get_element( $node );
		if ( $node->length > 1 ){
			foreach ( $node as $item ) {
				if ( !$this->fill_data( $item , $data ) ) return false;
			}
		}
		else {
			if ( !$this->fill_data( $node , $data ) ) return false;
		}
		return $this->save_file();
	}

	/**
	 * fill_data
	 *
	 * @param object $node
	 * @param string $data
	 * @return bool
	 */
	private function fill_data( $node, $data = '' )
	{
		if ( $sxml = @simplexml_load_string( $data ) ) {
			if ( !$dom_sxml = @dom_import_simplexml( $sxml ) ) return false;
			$dom_sxml = $this->importNode( $dom_sxml, true);
			if ( $node->childNodes->length > 1 ) {
				while ( $node->hasChildNodes() ) {
					if ( !$node->removeChild( $node->childNodes->item(0) ) ) return false;
				}
				if ( !$node->appendChild( $dom_sxml ) ) return false;
			}
			else {
				if ( !$node->replaceChild( $dom_sxml, $node->firstChild ) ) return false;
			}
		}
		else {
			$dom = new DOMDocument();
			if ( $dom->loadXML( "<data>". $data ."</data>" ) ){
				$root = $dom->documentElement;
				if ( $root->childNodes->length > 1 ) {
					foreach ( $root->childNodes as $entry ) {
						$sxml = simplexml_import_dom( $entry );
						if ( !$dom_sxml = @dom_import_simplexml( $sxml ) ) return false;
						$dom_sxml = $this->importNode( $dom_sxml, true);
						if ( $node->childNodes->length > 1 ) {
							while ( $node->hasChildNodes() ) {
								if ( !$node->removeChild( $node->childNodes->item(0) ) ) return false;
							}
							if ( !$node->appendChild( $dom_sxml ) ) return false;
						}
						else {
							if ( !$node->replaceChild( $dom_sxml, $node->firstChild ) ) return false;
						}
					}
				}
				else {
					if ( !$root->firstChild->tagName ) {
						if ( !$text = $this->createTextNode( $data ) ) return false;
						if ( $node->childNodes->length > 1 ) {
							while ( $node->hasChildNodes() ) {
								if ( !$node->removeChild( $node->childNodes->item(0) ) ) return false;
							}
							if ( !$node->appendChild( $text ) ) return false;
						}
						else {
							if ( !$node->replaceChild( $text, $node->firstChild ) ) return false;
						}
					}
					else {
						return false;
					}
				}
			}
			else {
				return false;
			}
		}
		return true;
	}

	/**
	 * 输出XML
	 *
	 * @return string
	 */
	function export()
	{
		if ( $this->sxml ) return $this->sxml->asXML();
		return $this->save_data();
	}

	/**
	 * 返回simplexml模型
	 *
	 * @return object
	 */
	function simplexml()
	{
		if ( $this->sxml ) return $this->sxml;
		return null;
	}

	/**
	 * 获取元素
	 *
	 * @param string $tag 选取的元素#id .class tag   [property="value"]
	 * @return object
	 */
	function get_element( $tag = '' )
	{
		if ( $tag ) {
			$aTag = preg_split( '/\[/', $tag );
			foreach ( $aTag as $key => $value ) {
				if ( $key == 0 ){
					if ( $value ){
						if ( preg_match('/^#/',$value) ){
							$query = '//*[@id="'. trim( $value, '#' ) .'"]';
						}
						elseif ( preg_match('/^\./',$tag) ) {
							$query = '//*[@class="'. trim( $value, '.' ) .'"]';
						}
						else {
							$query = '//'. $value;
						}
					}
					else {
						$query = '//*';
					}
				}
				else {
					$query .= '[@'. $value;
				}
			}
		}
		else {
			return $this->root;
		}
		if ( $query == '//*' ) return $this->root;
		$nodes = $this->query( $query );
		return $nodes;
	}

	/**
	 * 获取元素内的内容
	 *
	 * @param string $tag 选取的元素#id .class tag   [property="value"]
	 * @return mixed
	 */
	function get_content( $tag = '' )
	{
		if ( !$this->sxml ) return null;
		$node = $this->get_element( $tag );
		$aXml = array();
		if ( $node->length > 1 ){
			foreach ( $node as $entry ) {
				$aXml[] = $this->get_content_data( $entry );
			}
		}
		else {
			$aXml[] = $this->get_content_data( $node );
		}
		if ( count( $aXml ) > 1 ) {
			return $aXml;
		}
		return $aXml[0];
	}

	/**
	 *get_content_data
	 *
	 * @param object $node
	 * @return string
	 */
	private function get_content_data( $node )
	{
		$i = $node->childNodes->length;
		if ( $i > 1){
			$j = 0;
			while ( $j < $i ) {
				$xml .= $node->childNodes->item( $j )->tagName ? simplexml_import_dom( $node->childNodes->item( $j ) )->asXML() : $node->childNodes->item( $j )->nodeValue;
				$j++;
			}
		}
		else {
			$xml = $node->firstChild->tagName ? simplexml_import_dom( $node->firstChild )->asXML() : $node->firstChild->nodeValue;
		}
		return $xml;
	}

	/**
	 * 保存文件
	 *
	 * @return bool
	 */
	function save_file()
	{
		if ( $this->isSave ){
			if ( $this->isHtml ) {
				return $this->saveHTMLFile( $this->file );
			}
			else {
				return $this->save( $this->file );
			}
		}
		$this->save_data();
	}

	/**
	 * 保存数据
	 *
	 * @return bool
	 */
	function save_data()
	{
		if ( $this->isHtml ) {
			return $this->saveHTML();
		}
		else {
			return $this->saveXML();
		}
	}

	/**
	 * 加载数据
	 *
	 * @param string $string 数据
	 */
	function load_data( $string )
	{
		if ( $this->isHtml ) {
			if ( !$this->loadHTML( $string ) ) die();
		}
		else {
			if ( !$this->loadXML( $string ) ) die();
		}
	}

	/**
	 * 加载文件
	 *
	 * @param string $file 文件名
	 */
	function load_file( $file )
	{
		if ( $this->isHtml ) {
			if ( !$this->loadHTMLFile( $file ) ) die();
		}
		else {
			if ( !$this->load( $file ) ) die();
		}
	}

	/**
	 * 将数组转化为XML
	 *
	 * @param array $array 数组
	 * @param string $roottag 根元素标签名 默认为array
	 * @param int $depth 数组层次 
	 * 
	 * @return string
	 */
	function array_to_xml( $array = array(), $roottag = '', $depth = 0 )
	{
		if ( is_array( $array ) ) {
			foreach ( $array as $key => $value ){
				if ( is_array( $value ) ){
					$xml .= '<'. ( is_numeric( $key ) ? 'a_'. $depth .'_'. $key : $key ) .'>'. $this->array_to_xml( $value, $roottag, ($depth+1) ) .'</'. ( is_numeric( $key ) ? 'a_'. $depth .'_'. $key : $key ) .'>';
				}
				else {
					$xml .= '<'. ( is_numeric( $key ) ? 'a_'. $depth .'_'. $key : $key ) .'>'. $value .'</'. ( is_numeric( $key ) ? 'a_'. $depth .'_'. $key : $key ) .'>';
				}
			}
			if ($depth == 0) {
				$roottag or $roottag = 'array';
				$xml = '<'. $roottag .'>'. $xml .'</'. $roottag .'>';
			}
			return $xml;
		}
		return null;
	}

	/**
	 * 获取xml的数组
	 *
	 * @return array
	 */
	function get_array()
	{
		foreach ( (array )$this->sxml as $key => $value ) {
			if ( preg_match( '/^a_[0-9]_/', $key ) ) {
				$array[] = $this->xml_to_array( $value );
			}
			else {
				$array[ $key ] = $this->xml_to_array( $value );
			}
		}
		return $array;
	}

	/**
	 * 将simplexml内的项目转化为数组
	 *
	 * @param mixed $item simplexml项目
	 * @return mixed 
	 */
	function xml_to_array( $item )
	{
		if (  !is_string( $item ) ){
			$item = (array ) $item;
			foreach ( $item as $key => $value ) {
				if ( preg_match( '/^a_[0-9]_/', $key ) ) {
					$array[] = $this->xml_to_array( $value );
				}
				else {
					$array[ $key ] = $this->xml_to_array( $value );
				}
			}
			return $array;
		}
		else {
			return $item;
		}
	}
}
?>